package invoice.line;

import invoice.Invoice;

public abstract class Line implements Invoice {
    private String item;
    private int units;
    private double unitPrice;
    private double subtotal;
    public static double total;
    private Invoice component;
    private int num;

    public Line(String i, int u, double up){
        item=i;
        units=u;
        unitPrice=up;
        subtotal=u*up;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void setItem(String i){
        item=i;
    }

    public void setUnits(int u){
        units=u;
        setSubtotal();
    }

    public void setUnitPrice(double up){
        unitPrice=up;
        setSubtotal();
    }

    private void setSubtotal(){
        if(units!=0&&unitPrice!=0)
            subtotal=units*unitPrice;
    }

    public double getSubtotal(){
        return subtotal;
    }

    public void decorate(Invoice comp){
        component=comp;
    }

    public void print(){
        if(component!=null)
            component.print();
    }

    public void printLine(){
        System.out.println((num+1)+"  "+changeLength(item)+
                changeLength(units+"")+
                changeLength("$"+unitPrice+"")+
                changeLength("$"+subtotal+""));
    }
    private String changeLength(String str){
        StringBuilder sb=new StringBuilder(str);
        for(int i=sb.length();i<15;i++)
            sb.append(" ");
        return sb.toString();
    }
}